package cn.itcast.nems.code.generator;

import cn.itcast.nems.common.entity.BaseEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import org.apache.ibatis.annotations.Mapper;

import java.util.HashMap;
import java.util.Map;

class GeneratorCodeByDatabaseTable extends BaseGeneratorConfig {

    private final static String DB_URL = "172.17.0.78";
    private final static int DB_PORT = 3306;
    private final static String DB_NAME = "EMS";
    private final static String DB_USERNAME = "developer";
    private final static String DB_PASSWORD = "developerpw";

    private final static String AUTHOR = "刘曙";
    // private final static String PACKAGE = "cn.itcast.nems.studentrecord";

    private final static String TABLE_PREFIX = "e2";
    // private final static String MAVEN_SUB_MODULE_NAME = "emsv2-dao";

    private final static  String PACKAGE_NAME  = "studentrecord" ;

    // *mapper.xml 路径
    private static final String XML_PATH = getProjectPath() + "/emsv2-dao/src/main/resources/mappers/%s";
    // entity 路径
    private static final String ENTITY_PATH = getProjectPath() + "/emsv2-dao/src/main/java/cn/itcast/nems/%s/dao/entity";
    // mapper 路径
    private static final String MAPPER_PATH = getProjectPath() + "/emsv2-dao/src/main/java/cn/itcast/nems/%s/dao/mapper";
    // service 路径
    private static final String SERVICE_PATH = getProjectPath() + "/emsv2-service/src/main/java/cn/itcast/nems/%s/service";
    // serviceImpl 路径
    private static final String SERVICE_IMPL_PATH = getProjectPath() + "/emsv2-service/src/main/java/cn/itcast/nems/%s/service/impl";
    // controller 路径
    private static final String CONTROLLER_PATH = getProjectPath() + "/emsv2-web/src/main/java/cn/itcast/nems/%s/web/controller/b";

    public static void main(String[] args) {
        // 请输入表名，多个表名用,隔开
        // final String TABLE_NAMES = "e2_journal_test";

        String[] tableNames = {"e2_journal_refund_line"};
        String url = String.format("jdbc:mysql://%s:%s/%s?serverTimezone=Asia/Shanghai", DB_URL, DB_PORT, DB_NAME) ;
        FastAutoGenerator.create(url, DB_USERNAME, DB_PASSWORD)
                // 全局配置
                .globalConfig(builder -> builder
                                .author(AUTHOR)
                        //.enableSwagger()
                )
                // 包配置
                .packageConfig(builder -> builder
                        .parent("cn.itcast.nems.studentrecord")
                        .xml("mappers")
                        .entity("dao.entity")
                        .mapper("dao.mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .controller("web.controller.b")
                        .pathInfo(getPathInfo())
                )
                // 策略配置
                .strategyConfig(builder -> builder
                        .addInclude(tableNames)
                        .addTablePrefix(TABLE_PREFIX)
                        // entity
                        .entityBuilder()
                        .enableChainModel()
                        .enableLombok()
                        .enableRemoveIsPrefix()
                        .superClass(BaseEntity.class)
                        .addSuperEntityColumns(
                                "tenant_id",
                                "subtenant_id",
                                "created_by",
                                "created_datetime",
                                "modified_by",
                                "modified_datetime",
                                "deleted_by",
                                "deleted_datetime",
                                "deleted_flag"
                        )
                        .logicDeleteColumnName("deleted_flag")
                        // controller
                        .controllerBuilder()
                        .enableRestStyle()
                        .formatFileName("%sController")
                        // service
                        .serviceBuilder()
                        .superServiceClass(IService.class)
                        .formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImp")
                        // mapper
                        .mapperBuilder()
                        .enableBaseResultMap()
                        .mapperAnnotation(Mapper.class)
                        .enableBaseColumnList()
                        .superClass(BaseMapper.class)
                        .formatMapperFileName("%sMapper")
                        .formatXmlFileName("%sMapper")
                )
                .execute();
    }


//        DataSourceConfig dataSourceConfig = dataSourceBuild(DB_URL, DB_PORT, DB_NAME, DB_USERNAME, DB_PASSWORD);
//        GlobalConfig globalConfig = globalConfig(AUTHOR, MAVEN_SUB_MODULE_NAME);
//        PackageConfig packageConfig = packageConfig(PACKAGE, MAVEN_SUB_MODULE_NAME);
//        StrategyConfig strategyConfig = strategyConfig(TABLE_NAMES, TABLE_PREFIX);

//        AutoGenerator generator = new AutoGenerator(dataSourceConfig);
//        generator.global(globalConfig)
//                 .packageInfo(packageConfig)
//                 .strategy(strategyConfig)
//                 .execute();
//    }

    private static Map<OutputFile, String> getPathInfo() {
        Map<OutputFile, String> pathInfo = new HashMap<>(5);
        pathInfo.put(OutputFile.entity, String.format(ENTITY_PATH,PACKAGE_NAME));
        pathInfo.put(OutputFile.mapper, String.format(MAPPER_PATH,PACKAGE_NAME));
        pathInfo.put(OutputFile.service, String.format(SERVICE_PATH,PACKAGE_NAME));
        pathInfo.put(OutputFile.serviceImpl, String.format(SERVICE_IMPL_PATH,PACKAGE_NAME));
        pathInfo.put(OutputFile.controller, String.format(CONTROLLER_PATH,PACKAGE_NAME));
        pathInfo.put(OutputFile.xml, String.format(XML_PATH,PACKAGE_NAME));
        return pathInfo;
    }
}
